package gold.digger;

import gold.utils.InputUtil;

import java.util.*;

/**
 * Created by fanzhenyu02 on 2021/12/10.
 * common problem solver template.
 */
public class LC819 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public String mostCommonWord(String paragraph, String[] banned) {
            Set<String> bannedSet = new HashSet<String>();
            for (String word : banned) {
                bannedSet.add(word);
            }
            int maxFrequency = 0;
            Map<String, Integer> frequencies = new HashMap<String, Integer>();
            StringBuffer sb = new StringBuffer();
            int length = paragraph.length();
            for (int i = 0; i <= length; i++) {
                if (i < length && Character.isLetter(paragraph.charAt(i))) {
                    sb.append(Character.toLowerCase(paragraph.charAt(i)));
                } else if (sb.length() > 0) {
                    String word = sb.toString();
                    if (!bannedSet.contains(word)) {
                        int frequency = frequencies.getOrDefault(word, 0) + 1;
                        frequencies.put(word, frequency);
                        maxFrequency = Math.max(maxFrequency, frequency);
                    }
                    sb.setLength(0);
                }
            }
            String mostCommon = "";
            Set<Map.Entry<String, Integer>> entries = frequencies.entrySet();
            for (Map.Entry<String, Integer> entry : entries) {
                String word = entry.getKey();
                int frequency = entry.getValue();
                if (frequency == maxFrequency) {
                    mostCommon = word;
                    break;
                }
            }
            return mostCommon;
        }
    }


    public void run() {
        int[] arr = InputUtil.toIntegerArray("[1,2,3]");
        System.out.println(new Solution().toString());
    }

    public static void main(String[] args) throws Exception {
        LC819 an = new LC819();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
